using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kingdee.BOS.WebApi.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace KingdeeViewBillDemo
{
    
    public partial class FormForKingee : Form
    {
        public FormForKingee()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = null;
            if(textBox1.Text!="")
            {
                setData(textBox1.Text.Replace(" ", ""));
            }
            else
            {
                MessageBox.Show("单据编号不能为空！","警告");
            }

        }

        private void setData(string BillNo)
        {
            //K3CloudApiClient client = new K3CloudApiClient("http://121.37.13.15/k3cloud/");
            K3CloudApiClient client = ClientCreate.getClient();
            String loginResult = client.ValidateLogin("60d1833eb4c418", "Administrator", "xxxx", 2052);

            var resultType = JObject.Parse(loginResult)["LoginResultType"].Value<int>();

            //int resultType = LoginTool.Login(ClientCreate.getClient());

            //登录结果类型等于1，代表登录成功
            if (resultType == 1)
            {

                    string BillNoStr = "{\"Number\":\"" + BillNo + "\"}";
                    string BillResult = client.View("SAL_SaleOrder", BillNoStr);

                    Root root = JsonConvert.DeserializeObject<Root>(BillResult);

                    String r = JsonConvert.SerializeObject(root.Result);
                    ResultModel result = JsonConvert.DeserializeObject<ResultModel>(r);
                    textBox2.Text = result.Result.BillNo;
                    textBox3.Text = result.Result.Date;
                    textBox4.Text = result.Result.CustId.Name[1].Value;
                    textBox5.Text = result.Result.DocumentStatus;
                    this.dataGridView1.DataSource = result.Result.SaleOrderEntry;
                    label7.Text = this.dataGridView1.Rows.Count.ToString();
                    SetRowNum(this.dataGridView1.Rows.Count);

            }
        }
        private void SetRowNum(int count)
        {
            for(int i=0;i<count;i++)
            {
                
                this.dataGridView1.Rows[i].HeaderCell.Value = (i+1).ToString();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            QueryForm queryForm = new QueryForm();
            queryForm.setReturnBillno += new setDgdata(setData);
            queryForm.Show();
        }

        private void FormForKingee_Load(object sender, EventArgs e)
        {

        }
    }
}
